1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37 package net.sf.pmr.keopsframework.domain.validation;
38
39 import java.util.ArrayList;
40 import java.util.HashMap;
41 import java.util.Iterator;
42 import java.util.List;
43 import java.util.Locale;
44 import java.util.Map;
45
46 import org.springframework.context.MessageSource;
47 import org.springframework.context.NoSuchMessageException;
48
49 /***
50 * @author Arnaud Prost (arnaud.prost@gmail.com)
51 *
52 * TODO : sur les opération sur les field utiliser les commons-beanUtils pour vérifier les champs et accéder à leur valeur et renvoyer une exception s'il le champs n'est pas trouvé
53 * TODO : supprimer la dépendance avec Spring. Ne dépendre que du JDK voir des commons
54 */
55 public class ErrorsImpl implements Errors {
56
57
58 private MessageSource messageSource;
59
60 private List globalErrors;
61
62 private Map fieldErrors;
63
64 private List allErrors;
65
66 /***
67 * @param resourceBundleMessageSource
68 */
69 public ErrorsImpl(final MessageSource messageSource) {
70 super();
71 this.messageSource = messageSource;
72 this.globalErrors = new ArrayList();
73 this.fieldErrors = new HashMap();
74 this.allErrors = new ArrayList();
75 }
76
77 /***
78 * Inner class containing message parameters
79 * @author Arnaud Prost (arnaud.prost@gmail.com)
80 *
81 */
82 private class MessageParametersImpl implements MessageParameters {
83
84 private String errorCode;
85
86 private Object[] errorArguments;
87
88 /***
89 * @param errorCode
90 * @param errorArguments
91 */
92 public MessageParametersImpl(final String errorCode,
93 final Object[] errorArguments) {
94 this.errorCode = errorCode;
95 this.errorArguments = errorArguments;
96 }
97
98 public Object[] getErrorArguments() {
99 return errorArguments;
100 }
101 public String getErrorCode() {
102 return errorCode;
103 }
104 }
105
106
107 /***
108 * @see net.sf.pmr.keopsframework.domain.validation.Errors#hasErrors()
109 */
110 public boolean hasErrors() {
111 if (globalErrors.size() == 0 & fieldErrors.size() == 0) {
112 return false;
113 } else {
114 return true;
115 }
116 }
117
118 /***
119 * @see net.sf.pmr.keopsframework.domain.validation.Errors#getErrorCount()
120 */
121 public int getErrorCount() {
122
123 int totalErrors = globalErrors.size();
124
125 for (Iterator i = fieldErrors.values().iterator(); i.hasNext();) {
126
127 Object object = i.next();
128
129 if (object instanceof List) {
130 List list = (List) object;
131 totalErrors = totalErrors + list.size();
132 } else {
133 totalErrors = totalErrors + 1;
134 }
135 }
136
137 return totalErrors;
138
139 }
140
141 /***
142 * @see net.sf.pmr.keopsframework.domain.validation.Errors#getAllErrors(java.util.Locale)
143 */
144 public List getAllErrors(Locale locale) {
145
146 allErrors.clear();
147
148 for (Iterator i = globalErrors.iterator(); i.hasNext();) {
149
150 MessageParametersImpl messageParamaters = (MessageParametersImpl) i.next();
151 String message = getMessage(messageParamaters.getErrorCode(), messageParamaters.getErrorArguments(), locale);
152
153 allErrors.add(message);
154
155 }
156
157 for (Iterator i = fieldErrors.values().iterator(); i.hasNext();) {
158
159 List list = (List) i.next();
160
161 for (Iterator iList = list.iterator(); iList.hasNext();) {
162
163 MessageParametersImpl messageParamaters = (MessageParametersImpl) iList.next();
164 String message = getMessage(messageParamaters.getErrorCode(), messageParamaters.getErrorArguments(), locale);
165
166 allErrors.add(message);
167
168 }
169
170 }
171
172 return allErrors;
173 }
174
175
176
177 /***
178 * @see net.sf.pmr.keopsframework.domain.validation.Errors#getAllErrorsMessageParameters()
179 */
180 public List getAllErrorsMessageParameters() {
181
182 List listToreturn = new ArrayList();
183
184 listToreturn.addAll(globalErrors);
185
186 for (Iterator i = fieldErrors.values().iterator(); i.hasNext();) {
187
188 List list = (List) i.next();
189
190 listToreturn.addAll(list);
191
192 }
193
194 return listToreturn;
195 }
196
197
198 /***
199 * @see net.sf.pmr.keopsframework.domain.validation.Errors#hasGlobalErrors()
200 */
201 public boolean hasGlobalErrors() {
202 if (globalErrors.size() > 0) {
203 return true;
204 } else {
205 return false;
206 }
207 }
208
209 /***
210 * @see net.sf.pmr.keopsframework.domain.validation.Errors#hasFieldErrors(String)
211 */
212 public boolean hasFieldErrors(final String field) {
213 if (this.getFieldErrorCount(field) > 0) {
214 return true;
215 } else {
216 return false;
217 }
218 }
219
220 /***
221 * @see com.keops.domain.validation#getGlobalErrorCount()
222 */
223 public int getGlobalErrorCount() {
224 return globalErrors.size();
225 }
226
227
228 public String getGlobalError(Locale locale) {
229
230 String errorToReturn = null;
231
232 for (Iterator i = globalErrors.iterator(); i.hasNext();) {
233
234 MessageParametersImpl messageParamaters = (MessageParametersImpl) i.next();
235 errorToReturn = getMessage(messageParamaters.getErrorCode(), messageParamaters.getErrorArguments(), locale);
236
237 break;
238 }
239
240 return errorToReturn;
241
242 }
243
244
245 /***
246 * @see net.sf.pmr.keopsframework.domaon.validation.Errors#getGlobalErrors()
247 */
248
249 public List getGlobalErrors() {
250 return globalErrors;
251 }
252
253 public List getGlobalErrors(final Locale locale) {
254
255 List listToReturn = new ArrayList();
256
257 for (Iterator i = globalErrors.iterator(); i.hasNext();) {
258
259 MessageParametersImpl messageParamaters = (MessageParametersImpl) i.next();
260 String message = getMessage(messageParamaters.getErrorCode(), messageParamaters.getErrorArguments(), locale);
261
262 listToReturn.add(message);
263
264 }
265
266 return listToReturn;
267
268
269 }
270 /***
271 * @see com.keops.domain.validation#getFieldErrorCount(java.lang.String)
272 */
273 public int getFieldErrorCount(final String field) {
274
275 int fieldErrorsToReturn = 0;
276
277 List list = (List) fieldErrors.get(field);
278
279 if (list != null) {
280 fieldErrorsToReturn = list.size();
281 }
282
283 return fieldErrorsToReturn;
284
285 }
286
287 /***
288 * @see come.keops.domain.validation.Errors#getFieldError(java.lang.String)
289 */
290 public String getFieldError(final String field, final Locale locale) {
291
292 String fieldErrorToReturn = null;
293
294 List list = (List) fieldErrors.get(field);
295
296 if (list != null) {
297
298 for (Iterator i = list.iterator(); i.hasNext();) {
299
300 MessageParametersImpl messageParamaters = (MessageParametersImpl) i.next();
301 fieldErrorToReturn = getMessage(messageParamaters.getErrorCode(), messageParamaters.getErrorArguments(), locale);
302
303 break;
304 }
305 }
306
307 return fieldErrorToReturn;
308
309 }
310
311
312 /***
313 * @see come.keops.domain.validation.Errors#getFieldErrors(java.lang.String,
314 * java.util.Locale)
315 */
316 public List getFieldErrors(final String field, final Locale locale) {
317
318 List listToReturn = null;
319
320 List list = (List) fieldErrors.get(field);
321
322 if (list != null) {
323
324 listToReturn = new ArrayList();
325
326 for (Iterator i = list.iterator(); i.hasNext();) {
327
328 MessageParametersImpl messageParamaters = (MessageParametersImpl) i.next();
329 String message = getMessage(messageParamaters.getErrorCode(), messageParamaters.getErrorArguments(), locale);
330
331 listToReturn.add(message);
332
333 }
334 }
335
336 return listToReturn;
337
338 }
339
340 /***
341 * @see com.keops.domain.validation.Errors#rejectValue(String, String,
342 * Locale String)
343 */
344 public void rejectValue(final String field, final String errorCode,
345 final Object[] errorArguments) {
346
347
348 MessageParametersImpl messageParameters = new MessageParametersImpl(errorCode, errorArguments);
349
350 if (fieldErrors.containsKey(field)) {
351
352 List list = (List) fieldErrors.get(field);
353 list.add(messageParameters);
354
355 } else {
356 List list = new ArrayList();
357 list.add(messageParameters);
358 fieldErrors.put(field, list);
359 }
360
361 }
362
363 /***
364 * @see net.sf.pmr.keopsframework.domain.validation.Errors#rejectValue(String, String)
365 */
366 public void rejectValue(final String field, final String errorCode) {
367
368 rejectValue(field, errorCode, null);
369
370 }
371
372 /***
373 * @see net.sf.pmr.keopsframework.domain.validation.Errors#reject(String, Object[])
374 */
375 public void reject(final String errorCode, final Object[] errorArguments) {
376
377 MessageParametersImpl messageParameters = new MessageParametersImpl(errorCode, errorArguments);
378
379 globalErrors.add(messageParameters);
380
381 }
382
383 /***
384 * @see net.sf.pmr.keopsframework.domain.validation.Errors#rejectValue(String, String)
385 */
386 public void reject(final String errorCode) {
387
388 reject(errorCode, null);
389
390 }
391
392 private String getMessage(final String errorCode,
393 final Object[] errorArguments, final Locale locale) {
394
395 String messageToReturn = null;
396
397 try {
398 messageToReturn = messageSource.getMessage(errorCode,
399 errorArguments, locale);
400 } catch (NoSuchMessageException e) {
401 messageToReturn = e.getMessage();
402 }
403
404 return messageToReturn;
405
406 }
407
408 }